/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neuronalenetzeelearning.view.controlpanel;

import java.awt.event.ActionEvent;
import neuronalenetzeelearning.view.gui.*;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import neuronalenetzeelearning.model.Graph;
import neuronalenetzeelearning.view.help.HelpPanel;
import neuronalenetzeelearning.view.help.HelpWindowFactory;

/**
 *
 * @author Henning
 */
public class LearningRuleHebbPanel extends GraphPanelProperty {

    DefaultTableModel matrixModel;
    JComboBox InputVectorNumber;
    JTable inputTable;
    JPanel inputPanel;
    int columns = 2;

    public LearningRuleHebbPanel() {
        super();
        this.setBackground(background);
        this.setLayout(grid);
        this.setBorder(javax.swing.BorderFactory.createTitledBorder("Einstellungen: Hebbsche Lernregel"));

        //Set the look and feel
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.gridheight = 1;
        constraints.gridx = 1;
        constraints.gridy = 1;

        JLabel bord = new JLabel("------------------------------------------------------------------------------------------------------------------");
        add(bord, constraints);

        constraints.gridwidth = GridBagConstraints.RELATIVE;
        constraints.gridx = 1;
        constraints.gridy = 2;

        JLabel input = new JLabel("Wieviele Eingabevektoren sollen dem Netz übergebn werden?");
        add(input, constraints);

        constraints.gridwidth = GridBagConstraints.RELATIVE;
        constraints.gridx = 2;
        constraints.gridy = 2;

        InputVectorNumber = new JComboBox();
        InputVectorNumber.setModel(new DefaultComboBoxModel(new String[]{"1", "2", "3", "4", "5"}));
        add(InputVectorNumber, constraints);
        InputVectorNumber.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                InputVectorNumber.getSelectedIndex();
                updateInputVector(InputVectorNumber.getSelectedIndex() + 2, matrixModel, inputTable, inputPanel, constraints);
            }
        });

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridheight = 1;
        constraints.insets = new Insets(1, 1, 1, 1);
        constraints.gridx = 1;
        constraints.gridy = 3;

        matrixModel = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {
                if (column == 0) {
                    return false;
                } else {
                    return true;
                }
            }
        };
        matrixModel.setColumnCount(columns);

        matrixModel.setRowCount(graph.getLayer1Nodes().size());

        for (int i = 0; i < graph.getLayer1Nodes().size(); i++) {
            matrixModel.setValueAt(graph.getLayer1Nodes().get(i).getName(), i, 0);
        }
        inputPanel = new JPanel();
        inputTable = new JTable();
        inputTable.setModel(matrixModel);
        inputPanel.setBackground(background);
        inputPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Eingabevektoren"));
        inputPanel.add(inputTable);
        this.add(inputPanel, constraints);

        JPanel PropertyInputPanel = new JPanel();
        PropertyInputPanel.setLayout(grid);
        PropertyInputPanel.setBackground(background);
        PropertyInputPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Optionen: Eingabe erstellen"));
        automaticInputButton = new JButton("Eingabe gerenieren lassen");

        constraints.gridwidth = GridBagConstraints.RELATIVE;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        PropertyInputPanel.add(automaticInputButton, constraints);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridheight = 1;
        constraints.insets = new Insets(1, 1, 1, 1);
        constraints.gridx = 1;
        constraints.gridy = 4;

        this.add(PropertyInputPanel, constraints);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridx = 1;
        constraints.gridy = 5;

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        navigation.add(previous, constraints);
        navigation.add(getHelpButton(), constraints);
        navigation.add(next, constraints);

        this.add(navigation, constraints);

        this.setVisible(true);
    }

    private void updateInputVector(int columns, DefaultTableModel matrixModel, JTable inputTable, JPanel inputPanel, GridBagConstraints constraints) {
        this.remove(inputPanel);
        matrixModel.setColumnCount(columns);
        inputTable.setModel(matrixModel);
        inputPanel.add(inputTable);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridheight = 1;
        constraints.insets = new Insets(1, 1, 1, 1);
        constraints.gridx = 1;
        constraints.gridy = 3;
        this.add(inputPanel, constraints);
        this.updateUI();
        this.repaint();
    }

    public DefaultTableModel getDefaultTableModel() {
        return this.matrixModel;
    }

    @Override
    public void setGraph(Graph graph) {
        this.graph = graph;
    }

    @Override
    public void PanelUpdate() {
        matrixModel.setRowCount(graph.getLayer1Nodes().size());
        for (int i = 0; i < graph.getLayer1Nodes().size(); i++) {
            matrixModel.setValueAt(graph.getLayer1Nodes().get(i).getName(), i, 0);
        }
        this.updateUI();
        this.repaint();
    }

    public int getInputVectorNumber() {
        return this.InputVectorNumber.getSelectedIndex() + 1;
    }

    @Override
    public JTable getInputTable() {
        return this.inputTable;
    }

    @Override
    public void updateInputTable() {
        updateInputVector(InputVectorNumber.getSelectedIndex() + 2, matrixModel, inputTable, inputPanel, constraints);
    }

    public static void main(String[] args) {
        JFrame test = new JFrame();
        test.setPreferredSize(new Dimension(800, 800));
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Graph graph = new Graph();
        test.add(new LearningRuleHebbPanel());
        test.pack();
        test.setVisible(true);
    }

//    @Override
//    public ActionListener getActionListener() {
//        return new ActionListener() {
//
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                System.out.println("Help from Learning Rule Chooser Panel");
//            }
//        };
//
//    }

   @Override
    public ActionListener getActionListener() {
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                HelpWindowFactory.createHelpWindow(new HelpPanel("src/neuronalenetzeelearning/view/help/resources/hebbsche lernregel.png"));
            }
        };

    }
}
